﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DreamOnGameEngine.Localization
{
    /// <summary>
    /// Locilisation of any ingame strings
    /// </summary>
    public static class Localization
    {
        public static Dictionary<string, LocaleFile> m_strings;
        /// <summary>
        /// Path of the Language dir
        /// </summary>
        public static string PrefixDir { get; set; }
        /// <summary>
        /// Current Locale
        /// </summary>
        public static string Locale { get; set; }
         static Localization()
        {
            m_strings = new Dictionary<string, LocaleFile>();
        }
        /// <summary>
        /// Load a new Locale file
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static bool Load(string xml)
        {
            LocaleFile file = new LocaleFile();
            bool succ=file.Load(PrefixDir+Locale+"\\"+xml);
            if (succ)
                m_strings.Add(Path.GetFileNameWithoutExtension(xml), file);
            else
                return false;
            return true;
        }

        /// <summary>
        /// Removes a Locale File
        /// </summary>
        /// <param name="xml">File to remove</param>
        /// <returns><c>true</c>if the file was rmeoved successfully</returns>
        public static bool Remove(string xml)
        {
            bool rem = m_strings.ContainsKey(xml);
            if (rem == false)
                return false;
            m_strings.Remove(xml);
            return true;
        }
        /// <summary>
        /// Gets a value given by Key
        /// </summary>
        /// <param name="value">Key</param>
        /// <returns>Value as corresponding to Key</returns>
        public  static string Get(string value)
        {
            
            foreach (LocaleFile file in m_strings.Values)
            {
                string str = file[value];
                if (str != null)
                    return str;
            }
            return null;
        }
        /// <summary>
        /// Returns properly formated value corresponding to Key
        /// </summary>
        /// <param name="value">Key</param>
        /// <param name="values">Values for Formatting</param>
        /// <returns>Properly formatted value corresponding to Key</returns>
        public static string Get(string value, params string[] values)
        {
            string str = Get(value);
            if (str == null)
                return null;
            return string.Format(str, values);
        }
    }
}
